import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jiffy/jiffy.dart';
import 'package:safetymonitor/app/components/custom_app_bar.dart';
import 'package:safetymonitor/app/components/custom_date_type.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_file_upload_page.dart';
import 'package:safetymonitor/app/components/custom_modele/custom_title_chart_page.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/ext/ext.dart';
import 'package:safetymonitor/app/core/values/colors.dart';
import 'package:safetymonitor/modules/home/new_alarm_warning_page/completed/detail/controller.dart';

///已完成的详情
class CompletedDetailPage extends StatelessWidget {
  CompletedDetailPage({super.key});

  final controller = Get.put(CompletedDetailController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      appBar: CustomAppBar(
        backgroundColor: const Color(0xFF1847E3),
        title: Text(
          '详情',
          style: TextStyle(
              color: Colors.white, fontSize: 16.w, fontWeight: FontWeight.w500),
        ),
        leading: const BackButton(
          color: Colors.white,
        ),
      ),
      body: GetBuilder(
        init: controller,
        builder: (_) {
          return Container(
              color: const Color(0xFFF5F5F6),
              child: Stack(
                children: [
                  _top(),
                  _getNormalView(),
                ],
              ));
        },
      ),
    );
  }

  Widget _top() {
    return Container(
      height: 100.w,
      width: AppDimens.sh,
      color: const Color(0xFF1847E3),
    );
  }

  /// 基本信息
  Widget _getNormalView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: SingleChildScrollView(
        child: Column(
          children: [
            AppGaps.vGap16,
            CustomTitleChartPage(
              contentPadding:
                  EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
              title: controller.entity.companyName,
              titleRightWidget: controller.listController
                  .getStatueView(controller.entity.disposalStatus),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}级别:',
                      value: CustomDateType.getAlarmType(controller.entity.alarmLevel, controller.entity.dataType)),
                  _getTextView(title: '系统-类别:', value: controller.entity.categoryZh),
                  _getTextView(title: '报警类型:',
                      value: CustomDateType.getAlarmTypeByValue(controller.entity.dataType)),
                  _getTextView(title: '点位:', value: controller.entity.deviceCode),
                  _getTextView(title: '内部设置阈值:', value:'${controller.entity.internalVal}'),
                  _getTextView(title: '政府规定阈值:', value: '${controller.entity.governmentVal}'),
                  _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}值:',
                      value: '${controller.entity.val==0?'': controller.entity.val}'),
                  _getTextView(title: '报警时长:', value: '${controller.entity.alarmTimeMin}分钟'),
                  _getTextView(title: '报警时间:', value:'${controller.entity.alarmStartTime}'.millTS2Str()),

                  // _getTextView(title: '系统名称:', value: controller.entity.systemName),
                  // _getTextView(title: '监测类型:', value: controller.entity.categoryZh.split('-').last),
                  // _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}开始:',
                  //     value: controller.entity.alarmStartTime != 0 ? Jiffy.parseFromMillisecondsSinceEpoch(controller.entity.alarmStartTime).format(pattern: 'yyyy-MM-dd HH:mm:ss') : ''),
                  // _getTextView(title: '${CustomDateType.getDataType(controller.entity.dataType)}结束:',
                  //     value: controller.entity.alarmEndTime != 0 ? Jiffy.parseFromMillisecondsSinceEpoch(controller.entity.alarmEndTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'): ''),
                  // _getTextView(title: '报警结束:', value: DateTime.now().toStr),
                  // AppGaps.vGap6,
                ],
              ),
            ),
            AppGaps.vGap16,
            CustomTitleChartPage(
              contentPadding:
                  EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
              title: '督办信息',
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _getTextView(title: '督办人:', value: controller.getDistributeReturnRecordListEntity.undertakePersonName),
                  _getTextView(title: '督办时间:',value: controller.getDistributeReturnRecordListEntity.undertakeTime.millTS2Str()),
                ],
              ),
            ),
            AppGaps.vGap16,
            CustomTitleChartPage(
              contentPadding:
                  EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
              title: '派发信息',
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _getTextView(title: '派发部门:', value: controller.getDistributeReturnRecordListEntity.dispatchPersonDeptName),
                  _getTextView(title: '派发人:', value: controller.getDistributeReturnRecordListEntity.dispatchPersonName),
                ],
              ),
            ),
            AppGaps.vGap16,
            CustomTitleChartPage(
              contentPadding:
                  EdgeInsets.only(right: 16.w, left: 16.w, top: 16.w),
              title: '处置信息',
              child:  Column(
                children: controller.getDistributeReturnRecordListEntity.distributeList.map((e) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                     _getTextView(title: '处置人:', value: e.alarmDisposalRecordDO.sceneDisposalPersonName),
                     _getTextView(title: '处置时间:', value:controller.getDistributeReturnRecordListEntity.disposeTime!=0?Jiffy.parseFromMillisecondsSinceEpoch(controller.getDistributeReturnRecordListEntity.disposeTime).format(pattern: 'yyyy-MM-dd HH:mm:ss'):''),
                     _getTextView(title: '报警原因:', value: e.alarmDisposalRecordDO.reasonType),
                      _getTextView(title: '处置措施:', value: e.alarmDisposalRecordDO.reasonAnalysis),
                      CustomFileUploadPage(
                        isRequired: false,
                        isShowDivider: false,
                        isEnabled: false,
                        isChange: false,
                        uploadedFiles: e.alarmDisposalRecordDO.disposalMeasuresAnnex.isNotEmpty ? e.alarmDisposalRecordDO.disposalMeasuresAnnex.split(',') : [],
                        title: '附件：',
                        onCallBack: (value) {},
                      ),
                      AppGaps.vGap20,
                    ],
                  );
                }).toList(),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 获取字段显示
  Widget _getTextView({required String title, required String value}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xffC7CEE5),
              ),
            ),
            AppGaps.hGap6,
            Expanded(
                child: Text(
              value,
              style: TextStyle(
                fontSize: 14.sp,
                height: 1.2,
                fontWeight: FontWeight.w400,
                color: const Color(0xff1D3452),
              ),
            ))
          ],
        ),
        AppGaps.vGap10,
      ],
    );
  }
}
